// pages/orgDetails/orgDetails.js
const app = getApp();
var WxParse = require('../../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    permission: false,
    detail: {}
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  toPage(e) {
    var dataset = e.currentTarget.dataset;
    var url = dataset.turn;
    if (dataset.id) {
      url += "?id=" + dataset.id;
    }
    wx.navigateTo({
      url: url
    })
  },
  toheat: function (e) {
    var dataset = e.currentTarget.dataset;
    getApp().mtj.trackEvent('yqlw_org_heat');
    wx.navigateTo({
      url: '/pages/heat/heat?id=' + dataset.id
    })
  },
  detail: function() {
    var that = this;
    app.req({
      url: "orgmini/org/newGetOrgDetailedInfo",
      data: {
        orgId: that.data.id
      },
      success: function(res) {
        if (200 == res.code) {
          var detail = res.data;
          if (detail.orgTypeName=='其他'){
            wx.setNavigationBarTitle({
              title: '组织介绍'
            })
          }else{
            wx.setNavigationBarTitle({
              title: detail.orgTypeName+'介绍'
            })
          }
          if (res.data.contents.length != 0){
            for (var item of res.data.contents) {
              if (!item.contentType) {
                item.content = item.content.trim().replace(/<br>/g, "\n");
              }
            }
          }
          if (detail.webOrgUserList.length>6){
            var arr = []
            for( var i = 0 ; i< 6;i++){
              arr.push(detail.webOrgUserList[i])
            }
            detail.webOrgUserList = arr;
          }
          that.toggle("detail", detail);
          var rich = detail.orgIntroduce
          WxParse.wxParse('rich', 'html', rich, that, 5);
          // var permission = {};
          var permission = false;
          // permission.isAdmin = detail.isOrg * 1 == 0 && detail.isAdmin * 1 == 1;
          var userPermission = detail.managerAuthList;
          if (userPermission && userPermission.length > 0) {
            for (let i of userPermission) {
              if (userPermission[i]==11){
                permission = true
              }
              // permission[i] = 1;
              
            }
            // permission.hasItem = 1;
          }
          that.toggle("permission", permission);
          console.log('权限', that.data.permission)
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.toggle("id", options.id);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    app.getUserInfo(function(userInfo) {
      that.detail();
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this;
    app.getUserInfo(function(userInfo) {
      that.detail();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})